<template>
  <div>
    <el-form ref="formRef" :rules="rules" :model="form" label-width="80px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="姓名" prop="username">
            <el-input v-model="form.username" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" />
          </el-form-item>
          <el-button type="primary" @click="onSubmit">更新</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { AdddataDaoru, updateUserInfoRequest } from '@/api/emPloyees'
export default {
  name: '',
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.AdddataDaoruname()
  },
  methods: {
    // 获取员工基本信息(姓名与密码)
    async AdddataDaoruname() {
      try {
        const res = await AdddataDaoru(this.$route.query?.id || '')
        console.log(1111111111111111111111111, res)
        if (res.success) {
          // this.form.username = res.data.username
          // this.form.password = res.data.password
          this.form = res.data
        }
      } catch (err) {
        console.log('AdddataDaoru: ', err)
      }
    },
    // 保存员工基本信息请求
    async updateUserInfo() {
      try {
        const res = await updateUserInfoRequest(this.form)
        console.log(222222, res)
        if (res.success) {
          this.$message('更新成功')
          // back()就是会把你推倒上一级路由
          this.$router.back()
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('保存出错')
      }
    },
    onSubmit() {
      console.log(this.form)
      this.$refs.formRef.validate(val => {
        if (val) {
          this.updateUserInfo()
        }
      })
    }
  }
}
</script>
